﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text.RegularExpressions;
using System.Data;
using Newtonsoft.Json;

namespace Zgq.CommonTools
{
    /// <summary>
    /// 解析JSON，仿Javascript风格
    /// </summary>
    public static class JsonHelper
    {
        //public static string ScriptSerialize<T>(this T t)
        //{
        //    JavaScriptSerializer serializer = new JavaScriptSerializer();
        //    serializer.MaxJsonLength = Int32.MaxValue;
        //    return serializer.Serialize(t);
        //}

        //public static T ScriptDeserialize<T>(this string strJson)
        //{
        //    JavaScriptSerializer serializer = new JavaScriptSerializer();
        //    return serializer.Deserialize<T>(strJson);
        //}

        /// <summary>
        /// JSON.net反序列化
        /// </summary>
        public static T JsonDeserialize<T>(this string jsonString)
        {
            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            //jsetting.Converters.Add(new JavaScriptDateTimeConverter());//指定转化日期的格式
            jsetting.NullValueHandling = NullValueHandling.Ignore;//忽略空值
            jsetting.TypeNameHandling = TypeNameHandling.Auto;
            return JsonConvert.DeserializeObject<T>(jsonString, jsetting);
        }

        /// <summary>
        /// JSON.net序列化
        /// </summary>
        public static string JsonSerialize<T>(this T jsonObject)
        {
            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            //jsetting.Converters.Add(new JavaScriptDateTimeConverter());//指定转化日期的格式
            jsetting.NullValueHandling = NullValueHandling.Ignore;//忽略空值
            jsetting.TypeNameHandling = TypeNameHandling.Auto;

            return JsonConvert.SerializeObject(jsonObject, Formatting.None, jsetting);
        }



        /// <summary>
        /// JSON.net反序列化
        /// </summary>
        public static T JsonDeserializeNoTypeNameHandling<T>(this string jsonString)
        {
            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            //jsetting.Converters.Add(new JavaScriptDateTimeConverter());//指定转化日期的格式
            jsetting.NullValueHandling = NullValueHandling.Ignore;//忽略空值
            return JsonConvert.DeserializeObject<T>(jsonString, jsetting);
        }

        /// <summary>
        /// JSON.net序列化
        /// </summary>
        public static string JsonSerializeNoTypeNameHandling<T>(this T jsonObject)
        {
            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            //jsetting.Converters.Add(new JavaScriptDateTimeConverter());//指定转化日期的格式
            jsetting.NullValueHandling = NullValueHandling.Ignore;//忽略空值

            return JsonConvert.SerializeObject(jsonObject, Formatting.None, jsetting);
        }

        public static string JsonSerializeByDataSet(this DataSet ds)
        {
            Dictionary<string, List<Dictionary<string, object>>> dsDics = ds.DataSetToDictionAryList(true);

            return dsDics.JsonSerializeNoTypeNameHandling();
        }

        public static DataSet JsonDeserializeByDataSet(this string jsonString)
        {
            var result = JsonDeserializeNoTypeNameHandling<Dictionary<string, List<Dictionary<string, object>>>>(jsonString);

            return result.ToDataSet();
        }


        /// <summary>
        /// JSON.net序列化
        /// </summary>
        public static string JsonSerializeByDataTable(this DataTable dt)
        {
            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            //jsetting.Converters.Add(new JavaScriptDateTimeConverter());//指定转化日期的格式
            jsetting.NullValueHandling = NullValueHandling.Ignore;//忽略空值

            if (dt == null)
                return "";

            List<Dictionary<string, object>> dicList = dt.ToDictionAryList(true);

            //var dicList = new List<Dictionary<string, object>>();

            //Dictionary<string, object> result = new Dictionary<string, object>();

            //foreach (DataColumn dc in dt.Columns)
            //{
            //    result.Add(dc.ColumnName, dc.DataType);
            //}
            //dicList.Add(result);

            //foreach (DataRow dr in dt.Rows)
            //{
            //    result = new Dictionary<string, object>();

            //    foreach (DataColumn dc in dt.Columns)
            //    {
            //        result.Add(dc.ColumnName, dr[dc]);
            //    }
            //    dicList.Add(result);
            //}

            return JsonConvert.SerializeObject(dicList, Formatting.None, jsetting);
        }


        /// <summary>
        /// JSON.net反序列化
        /// </summary>
        public static DataTable JsonDeserializeNoTypeNameHandling(this string jsonString)
        {
            JsonSerializerSettings jsetting = new JsonSerializerSettings();
            //jsetting.Converters.Add(new JavaScriptDateTimeConverter());//指定转化日期的格式
            jsetting.NullValueHandling = NullValueHandling.Ignore;//忽略空值
            var result = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonString, jsetting);


            return result.ToDataTable();
        }
    }
}